package com.spa.interfaces.admin;

import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.spa.application.command.admin.ReplyCommentCommand;
import com.spa.application.command.admin.TopCommentCommand;
import com.spa.application.dto.PageResult;
import com.spa.application.dto.admin.DouYinCommentDTO;
import com.spa.application.dto.admin.DouYinOauthDTO;
import com.spa.application.query.PageQuery;
import com.spa.application.query.douyin.CommentQuery;
import com.spa.application.query.douyin.IFrameQuery;
import com.spa.application.query.douyin.PoiQuery;
import com.spa.application.query.douyin.VideoQuery;
import com.spa.application.service.admin.DouYinApplicationService;
import com.spa.domain.entity.DouYinOauth;
import com.spa.infrastructure.common.ResponseMessage;
import com.spa.infrastructure.service.douyin.DouYinWebService;
import com.spa.infrastructure.service.douyin.result.web.ItemCommentReplyResult;
import com.spa.infrastructure.service.douyin.result.web.ItemCommentTopResult;
import com.spa.infrastructure.service.douyin.result.web.VideoListResult;
import com.spa.interfaces.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("admin/dy")
public class AdminDouYinController extends BaseController {

    @Autowired
    private DouYinApplicationService douYinApplicationService;

    @Autowired
    private DouYinWebService douYinWebService;

    @PostMapping("pageDouYinUser")
    public ResponseMessage<PageResult<DouYinOauthDTO>> pageDouYinUser(@RequestBody PageQuery query) throws JsonProcessingException {
        return responseHelper.success(douYinApplicationService.index(query));
    }

    @PostMapping("getVideoList")
    public ResponseMessage<List<VideoListResult.Video>> getVideoList(@RequestBody VideoQuery query) throws JsonProcessingException {
        DouYinOauth douYinOauth = douYinWebService.getAccessToken(query.getId());
        if (ObjectUtil.isNull(douYinOauth)) {
            return responseHelper.fail("请重新授权！");
        }
        VideoListResult videoListResult = douYinWebService.getVideoList(douYinOauth.getAccessToken(), douYinOauth.getOpenId(), 0);
        return responseHelper.success(videoListResult.getData().getList());
    }

    @PostMapping("getIframeByItemId")
    public ResponseMessage<String> getIframeByItemId(@RequestBody IFrameQuery query) throws JsonProcessingException {
        String res = douYinWebService.getIframeByItemId(query.getItemId());
        return responseHelper.success(res);
    }

    @PostMapping("getItemCommentList")
    public ResponseMessage<PageResult<DouYinCommentDTO>> getItemCommentList(@RequestBody CommentQuery query) throws JsonProcessingException {
        return responseHelper.success(douYinApplicationService.pageDouYinComment(query));
    }

    @PostMapping("getSecondaryItemCommentList")
    public ResponseMessage<PageResult<DouYinCommentDTO>> getSecondaryItemCommentList(@RequestBody CommentQuery query) throws JsonProcessingException {
        return responseHelper.success(douYinApplicationService.pageSecondaryDouYinComment(query));
    }

    @PostMapping("replyComment")
    public ResponseMessage<ItemCommentReplyResult> replyComment(@RequestBody ReplyCommentCommand command) throws JsonProcessingException {
        DouYinOauth douYinOauth = douYinWebService.getAccessToken(command.getId());
        if (ObjectUtil.isNull(douYinOauth)) {
            return responseHelper.fail("请重新授权！");
        }
        ItemCommentReplyResult itemCommentReplyResult = douYinWebService.replyComment(douYinOauth.getAccessToken(), command.getCommentId(),
                command.getItemId(), command.getContent(), douYinOauth.getOpenId());
        return responseHelper.success(itemCommentReplyResult);
    }

    @PostMapping("topComment")
    public ResponseMessage<ItemCommentTopResult> topComment(@RequestBody TopCommentCommand command) throws JsonProcessingException {
        DouYinOauth douYinOauth = douYinWebService.getAccessToken(command.getId());
        if (ObjectUtil.isNull(douYinOauth)) {
            return responseHelper.fail("请重新授权！");
        }
        ItemCommentTopResult itemCommentTopResult = douYinWebService.topComment(douYinOauth.getAccessToken(), command.getCommentId(),
                command.getItemId(), command.isTop(), douYinOauth.getOpenId());
        return responseHelper.success(itemCommentTopResult);
    }

    @PostMapping("poiSearch")
    public ResponseMessage<List<VideoListResult.Video>> poiSearch(@RequestBody PoiQuery query) throws JsonProcessingException {
        String itemCommentListResult = douYinWebService.poiSearch(query.getKeyword(), 0, query.getSize().intValue(), query.getCity());
        return responseHelper.success(itemCommentListResult);
    }

}
